﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyRead.API.Appicant.Command.Approval;
using B.S.DataBase.MyRead.API.DTOs;
using MediatR;

namespace B.S.DataBase.MyRead.API.Appicant.CommandHandler.Approval
{
    public class GetDepHeadsCommendHandlers : IRequestHandler<GetDepHeadsCommends, APIResult<List<User>>>
    {

        private readonly IBaseRepository<User> userRepository;
        private readonly IMapper mapper;

        public GetDepHeadsCommendHandlers(IBaseRepository<User> userRepository, IMapper mapper)
        {
            this.userRepository = userRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 根据部门ID获取部门领导
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<APIResult<List<User>>> Handle(GetDepHeadsCommends request, CancellationToken cancellationToken)
        {
            APIResult<List<User>> result= new APIResult<List<User>>();
            result.Code = APIEnum.查询成功;
            result.Msg=APIEnum.查询成功.ToString();
            var depHeads = userRepository.GetAll().Where(x => x.DepId == request.DepId).ToList();
            if (depHeads.Count()>0) {
                if (request.Heads < 2)
                {
                    depHeads = depHeads.Where(x => x.Heads == (request.Heads + 1)).ToList();
                }
                else
                {
                    depHeads = depHeads.Where(x => x.Heads == 2).ToList();
                }
            }

            result.data= depHeads;
        
            return Task.FromResult(result);
        }
    }
}
